import router from "./router";
import { getToken } from "./utils/auth";
import { Message } from "element-ui";
import store from "./store";
import NProgress from 'nprogress'

const whiteList = ['/404','/login']
router.beforeEach((to,from,next)=>{
    const token = getToken()
    NProgress.start()
    // console.log(token);
    if(token){
        if(!store.getters.userInfo.username){
            store.dispatch('user/getUserInfo')
        }           
        if(to.path === '/login'){
            Message.warning('已登录,返回首页')
            NProgress.done()
            next('/')
        }else{
            next()
        }
    }else{
        if(whiteList.includes(to.path)){
            next()
        }else{
            Message.warning('未登录,请登陆后操作')
            next(`/login?_redirect=${to.path}`)
        }
    }
})
router.afterEach((to,from,next)=>{
    NProgress.done()
})